//******************************************************************************
/**
 *
 * @file	slot_main.c
 * @brief	ミニゲーム　スロット　メイン
 * @author	iwasawa
 * @data	08.11.22
 *
 */
//******************************************************************************
#include "common.h"
#include "system/procsys.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/snd_tool.h"
#include "system/fontproc.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/buflen.h"
#include "system/window.h"
#include "system/brightness.h"
#include "system/clact_util.h"
#include "system/clact_tool.h"
#include "system/render_oam.h"
#include "system/palanm.h"
#include "system/wipe.h"
#include "system/window.h"
#include "system/font_arc.h"
#include "system/winframe.naix"
#include "gflib/touchpanel.h"
#include "gflib/strbuf_family.h"

#include "application/app_tool.h"

#include "slot_local.h"
#include "slot_main.h"
#include "slot_sub.h"
#include "slot_reel.h"

#ifdef PM_DEBUG
#ifdef DEBUG_ONLY_FOR_iwasawa
//#define DEBUG_MODE_CHANGE_FORCE	//有効にすると強制モードチェンジ抽選
#endif
#endif

//--------------------------------------------------------------
///	ゲームプロセス戻り値
//--------------------------------------------------------------
typedef enum
{
	RET_NONE = 0,///<特に無し
	RET_NEXT,	///<次へ
	RET_END,	///<終了
}GAMERET;

//--------------------------------------------------------------
///	入力コード
//--------------------------------------------------------------
typedef enum
{
	INPUT_NONE,	///<特に無し
	INPUT_EXE,	///<実行
	INPUT_END,	///<終了
}INPUT_RET;

//--------------------------------------------------------------
///	ゲームシーケンス
//--------------------------------------------------------------
enum
{
	//通常ゲーム
	SEQ_GAME_INIT = 0,
	SEQ_GAME_INIT_EFF,
	SEQ_GAME_BET,
	SEQ_GAME_START_WAIT,
	SEQ_GAME_START,
	SEQ_GAME_REEL_WAIT0,
	SEQ_GAME_REEL_WAIT1,
	SEQ_GAME_REEL_WAIT2,
	SEQ_GAME_REEL_SCROLL_WAIT,
	SEQ_GAME_PAYOUT,
	SEQ_GAME_LOSE_EFF,
	SEQ_GAME_MODE_CHANGE,
	SEQ_GAME_END,	//1ゲーム終了
	SEQ_TO_BONUS,	//ボーナスゲーム画面へ切替
	SEQ_TO_NORMAL,	//通常ゲーム画面へ切替
	SEQ_BONUS_INIT,
	SEQ_BONUS_BET,
	SEQ_BONUS_START_WAIT,
	SEQ_BONUS_START,
	SEQ_BONUS_AUTO_REEL_WAIT,
	SEQ_BONUS_REEL_WAIT,
	SEQ_BONUS_REEL_SCROLL_WAIT,
	SEQ_BONUS_PAYOUT,
	SEQ_BONUS_LOSE_EFF,
	SEQ_BONUS_END,	//1ゲーム終了
	SEQ_GAME_EXIT_CHECK,
	SEQ_GAME_FORCE_END,
	SEQ_GAME_EXIT,
	SEQ_MAX,
};

static GAMERET (* const DATA_SlotGameTbl[SEQ_MAX])( SLOTMAIN *wk );


//==============================================================================
//	スロット　プロトタイプ
//==============================================================================
static BOOL gsub_IsContinue( SLOTMAIN* wk ,BOOL is_bonus);
static void gsub_StateChange( SLOTMAIN* wk, GAME_STATE state);
static void gsub_Lottery(SLOTMAIN* wk);
static int gsub_ReachCheck( SLOTMAIN* wk );
static void gsub_BonusLottery(SLOTMAIN* wk);
static int gsub_GradIconNumCheck( SLOTMAIN* wk );
static int gsub_DisconIconNumCheck( SLOTMAIN* wk );

static void sub_YesNoBtnSet(SLOTMAIN* wk);
static int sub_YesNoBtnWait(SLOTMAIN* wk);


#ifdef PM_DEBUG
static void deb_AutoPlayMain(SLOTMAIN* wk)
{
	if(!wk->deb_auto_f){
		return;
	}
	if((sys.trg & PAD_BUTTON_L)){	//解除
		wk->deb_auto_ct = 0;
		wk->deb_auto_f = FALSE;
		return;
	}
	if(wk->deb_auto_ct++ % 30 > 0){
		return;
	}
	sys.tp_trg = TRUE;
	sys.tp_x = 128;
	sys.tp_y = 128;
	if(wk->credit < 10){
		wk->credit = 10000;
	}
}
#endif	//PM_DEBUG

//==============================================================================
//	スロット　ゲームメイン
//==============================================================================
//--------------------------------------------------------------
/**
 * スロットゲーム処理
 * @param	main	SLOTMAIN *
 * @retval	int		TRUE=終了
 */
//--------------------------------------------------------------
int SlotMain_Game( SLOTMAIN *wk )
{
	GAMERET ret;

	do{
#ifdef PM_DEBUG
		deb_AutoPlayMain(wk);
#endif	//PM_DEBUG

		ret = DATA_SlotGameTbl[wk->game_seq]( wk );
	}while( ret == RET_NEXT );

	if( ret == RET_END ){
		return( TRUE );
	}
	return( FALSE );
}

//==============================================================================
//	スロット　入力取得
//==============================================================================
static const RECT_HIT_TBL TpBackButtonRect = {
	//	{0,191,0,255}, ty,by,lx,rx
	 TP_BACK_Y,TP_BACK_Y+TP_BACK_H,TP_BACK_X,TP_BACK_X+TP_BACK_W
};

static INPUT_RET Input_Check(SLOTMAIN* wk,int exe_key)
{
	if(GF_TP_GetTrg()){
		wk->key_mode = APP_KTST_TOUCH;
		if(	(!wk->back_btn_passive_f) &&
			GF_TP_SingleHitTrg( &TpBackButtonRect )){
			Snd_SePlay(SND_SLOT_CANCEL);
			return INPUT_END;
		}
		return INPUT_EXE;
	}else{
		if(sys.trg & PAD_BUTTON_KTST_CHG){
			wk->key_mode = APP_KTST_KEY;
		}
		if(	(!wk->back_btn_passive_f) &&
			sys.trg & PAD_BUTTON_CANCEL){
			Snd_SePlay(SND_SLOT_CANCEL);
			return INPUT_END;
		}
		if( sys.trg & exe_key){
			return INPUT_EXE;
		}
	}
	return INPUT_NONE;
}

//==============================================================================
//	スロット　ゲームシーケンス
//==============================================================================

//--------------------------------------------------------------
/**
 * ゲーム　ゲームシーケンスの切替
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static void GameSeq_Change( SLOTMAIN *wk ,int seq)
{
	wk->game_seq = seq;
	wk->game_next_seq = SEQ_GAME_INIT;
	wk->sub_seq = 0;
}
static void GameSeq_ChangeNext( SLOTMAIN *wk ,int seq,int next)
{
	wk->game_seq = seq;
	wk->game_next_seq = next;
	wk->sub_seq = 0;
}

static GAMERET Game_Null( SLOTMAIN *wk )
{
	return RET_NONE;
}

//--------------------------------------------------------------
/**
 * ゲーム　ゲーム初期化
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_Init( SLOTMAIN *wk )
{
	wk->payout = 0;
	wk->bet = 0;
	wk->reel_speed = REEL_SPEED_DEF;
	wk->bonus_reach = REACH_CODE_BLANK;
	wk->next_mode = MODE_BLANK;

	wk->bonus_game_f = FALSE;
	wk->game_cancel_f = FALSE;
	wk->super_reach_f = FALSE;

	if(!wk->replay_f){
		SlotSub_SysMsgPut(wk,MIDX_SYS_BET);
		SlotSub_BackBtnWrite(wk,BACK_BTN_ACTIVE);
	}

	GameSeq_Change( wk, SEQ_GAME_INIT_EFF );
	return( RET_NEXT );
}

//--------------------------------------------------------------
/**
 * ゲーム　ゲーム初期化
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_InitEff( SLOTMAIN *wk )
{
	if(wk->stress_ct != 0){
		switch(wk->sub_seq){
		case 0:
			 SlotSub_EmotionIconSet( wk );
			 wk->sub_seq++;
			return RET_NONE;
		case 1:
			if(wk->eff_tcb_ct){
				return RET_NONE;
			}
			break;
		}
	}
	if(wk->replay_f){
		wk->replay_f = FALSE;
		GameSeq_Change( wk ,SEQ_GAME_START);
		return RET_NONE;
	}else{
		GameSeq_Change( wk ,SEQ_GAME_BET);
	}
	return( RET_NEXT );
}

//--------------------------------------------------------------
/**
 * ゲーム	ベット
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_Bet( SLOTMAIN *wk )
{
	INPUT_RET ret;

	ret = Input_Check(wk,PAD_KEY_UP);
	if(ret == INPUT_NONE){
#ifdef PM_DEBUG
		Debug_WinVisibleCheck(wk);
#endif
		return RET_NONE;
	}
	if(ret == INPUT_END){
		GameSeq_ChangeNext( wk ,SEQ_GAME_EXIT_CHECK,SEQ_GAME_BET);
		return RET_NONE;
	}
	wk->bet = COIN_BET_DEF;
	wk->credit -= wk->bet;
	SlotSub_CoinNumDraw(wk,wk->credit,COIN_NUM_CREDIT);
	SlotSub_BetLineLightSetAll( wk, LIGHT_ON );
	SlotSub_ReelLampSetAll( wk, LIGHT_ON );

	//残っている絵を消去
	wk->eff_tcb_ct = 0;
	AddTCB_PictOff( wk, &wk->eff_tcb_ct, TCBPRI_MONITER_EFF );

	SlotSub_SysMsgPut(wk,MIDX_SYS_START);
	GameSeq_Change( wk ,SEQ_GAME_START_WAIT);
	Snd_SePlay(SND_SLOT_BET);

#ifdef PM_DEBUG
//	wk->deb_play_ct++;
#endif	//PM_DEBUG
	return( RET_NONE );
}

//--------------------------------------------------------------
/**
 * ゲーム	ゲーム開始待ち
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_StartWait( SLOTMAIN *wk )
{
	INPUT_RET ret;

	if(wk->eff_tcb_ct){
		return RET_NONE;
	}
	ret = Input_Check(wk,(PAD_KEY_UP|PAD_KEY_DOWN));
	if(ret == INPUT_NONE){
#ifdef PM_DEBUG
		Debug_WinVisibleCheck(wk);
#endif
		return RET_NONE;
	}
	if(ret == INPUT_END){
		wk->game_cancel_f = TRUE;
		GameSeq_ChangeNext( wk ,SEQ_GAME_EXIT_CHECK, SEQ_GAME_START_WAIT);
		return RET_NONE;
	}
	//抽選
	GameSeq_Change( wk ,SEQ_GAME_START);
	SlotSub_BackBtnWrite(wk,BACK_BTN_PASSIVE);
	return( RET_NEXT );
}

//--------------------------------------------------------------
/**
 * ゲーム	ゲーム開始
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_Start( SLOTMAIN *wk )
{
	INPUT_RET ret;

	//役抽選
	gsub_Lottery(wk);

#ifdef PM_DEBUG
	Debug_LotterySet(wk);
	Debug_WinUpdate(wk);
	IWASAWA_PRINT("lot = %d : %d\n",wk->lot_basic,wk->lot_ex);
#endif

	//リール回転スタート
	ReelControlLotteryTargetSet( wk );
	ReelControlScrollSet( wk, 0);
	ReelControlScrollSet( wk, 1);
	ReelControlScrollSet( wk, 2);

	SlotSub_DooburuAnmSet( wk, DOOBURU_F2B );
	AddTCB_ReelLampAnimeSet( wk,REEL_0,TCBPRI_REEL_LAMP_ANM);

	SlotSub_SysMsgPut(wk,MIDX_SYS_STOP);
	GameSeq_Change( wk ,SEQ_GAME_REEL_WAIT0);
	Snd_SePlay(SND_SLOT_REEL_START);
	return( RET_NONE );
}

//--------------------------------------------------------------
/**
 * ゲーム	リール一本目ストップ待ち
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_ReelWait0( SLOTMAIN *wk )
{
	INPUT_RET ret;

	switch(wk->sub_seq){
	case 0:
#ifdef PM_DEBUG
		Debug_WinVisibleCheck(wk);
#endif
		ret = Input_Check(wk,PAD_BUTTON_A);
		if(ret == INPUT_EXE){
			ReelControlStopSet( wk, 0);
			SlotSub_ReelLampAnimeStopReq( wk, REEL_0 );
			AddTCB_ReelLampAnimeSet( wk,REEL_1,TCBPRI_REEL_LAMP_ANM);
			wk->sub_seq++;
		}
		break;
	case 1:
		if(wk->reel[REEL_0].scroll_flag){
			return RET_NONE;
		}
		GameSeq_Change( wk ,SEQ_GAME_REEL_WAIT1);
		break;
	}
	return( RET_NONE );
}
//--------------------------------------------------------------
/**
 * ゲーム	リール二本目ストップ待ち
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_ReelWait1( SLOTMAIN *wk )
{
	INPUT_RET ret;

	switch(wk->sub_seq){
	case 0:
#ifdef PM_DEBUG
		Debug_WinVisibleCheck(wk);
#endif
		ret = Input_Check(wk,PAD_BUTTON_A);
		if(ret == INPUT_EXE){
			ReelControlStopSet( wk, 1);
			SlotSub_ReelLampAnimeStopReq( wk, REEL_1 );
			wk->sub_seq++;
		}
		break;
	case 1:
		if(wk->reel[REEL_1].scroll_flag){
			return RET_NONE;
		}
		//リーチチェック
		{
			int ret;

			ret = gsub_ReachCheck( wk );

			if(ret == 0){
				GameSeq_Change( wk ,SEQ_GAME_REEL_WAIT2);
				return  RET_NONE;
			}
		}
		//リーチエフェクト登録
		wk->eff_tcb_ct = 0;
		AddTCB_ExIconEff( wk,wk->super_reach_f,NULL,TCBPRI_MONITER_EFF);
		if(wk->super_reach_f){
			AddTCB_DraftDraw( wk,wk->bonus_reach+PICT_BALL,&wk->eff_tcb_ct,TCBPRI_MONITER_EFF);
		}
		wk->sub_seq++;
		break;
	case 2:	//リーチエフェクト待ち
		if(wk->eff_tcb_ct){
			return RET_NONE;
		}
		GameSeq_Change( wk ,SEQ_GAME_REEL_WAIT2);
		break;
	}

	return( RET_NONE );
}

//--------------------------------------------------------------
/**
 * ゲーム	リール三本目スタート→ストップ待ち
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_ReelWait2( SLOTMAIN *wk )
{
	INPUT_RET ret;

	switch(wk->sub_seq){
	case 0:
		AddTCB_ReelLampAnimeSet( wk,REEL_2,TCBPRI_REEL_LAMP_ANM);
		wk->sub_seq++;
		break;
	case 1:
#ifdef PM_DEBUG
		Debug_WinVisibleCheck(wk);
#endif
		ret = Input_Check(wk,PAD_BUTTON_A);
		if(ret == INPUT_EXE){
			ReelControlStopSet( wk, 2);
			SlotSub_ReelLampAnimeStopReq( wk, REEL_2 );
			wk->sub_seq++;
		}
		break;
	case 2:
		if(wk->reel[REEL_2].scroll_flag){
			return RET_NONE;
		}
		GameSeq_Change( wk ,SEQ_GAME_REEL_SCROLL_WAIT);
		break;
	}
	return( RET_NONE );
}

//--------------------------------------------------------------
/**
 * ゲーム	リールの動作エフェクトが終了するまで待つ
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_ReelScrollWait( SLOTMAIN *wk )
{
	int i;

	for(i = 0;i < REEL_MAX;i++){
		if(wk->reel[i].scroll_flag){
			return( RET_NONE );
		}
	}
	//Win/Loseチェック
	{
		int num,code[LINE_CODE_MAX];
		num = ReelCheck(wk,REEL_CHECK_WIN,code,wk->hit_line);
		if(num == 0){
			wk->win_code = PRIZE_BLANK;
		}else{
#if	PM_DEBUG
			if(num > 1){
				GF_ASSERT_MSG(0,"Win over 2line%d\n",num);
			}
#endif
			wk->win_code = code[0];
			wk->win_line = wk->hit_line[0];
		}
	}
	SlotSub_IconEffVanishAll(wk);

#ifdef PM_DEBUG
	Debug_WinResultSet(wk);
#endif
	if(wk->win_code == PRIZE_BLANK){
		GameSeq_Change( wk ,SEQ_GAME_LOSE_EFF);
		return( RET_NONE );
	}else if(wk->win_code == PRIZE_REPLAY){
		wk->replay_f = TRUE;
	}
	GameSeq_Change( wk, SEQ_GAME_PAYOUT );
	return( RET_NEXT );
}

//--------------------------------------------------------------
/**
 * ゲーム	コイン払い戻し
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_Payout( SLOTMAIN *wk )
{
	int i;

	switch(wk->sub_seq){
	case 0:
		//お絵かき開始
		AddTCB_PictDraw(wk,wk->win_code,&wk->eff_tcb_ct,TCBPRI_PAYOUT);
		AddTCB_GuideBlinkEff(wk,STATE_NORMAL, wk->win_code ,&wk->eff_tcb_ct,TCBPRI_PAYOUT);
		AddTCB_HitLineEff(wk,wk->win_line,NULL,TCBPRI_PAYOUT);

		if( SlotSub_IsBonusCode(wk->win_code)){
			AddTCB_BguBackChange(wk,BGU_BACK_BIG,&wk->eff_tcb_ct);
		}
		wk->sub_seq++;
		break;
	case 1:
		//お絵かき終了待ち
		if(wk->eff_tcb_ct){
			return RET_NONE;
		}
		//喜びアイコン表示セット
		SlotSub_WinLoseIconSet( wk, gsub_GradIconNumCheck(wk), TRUE );

		//コイン払い戻しエフェクトタスク追加
		if(!wk->replay_f){
			AddTCB_Payout(wk,
				SlotSub_GetPayoutCoinNum(wk,wk->win_code),&wk->eff_tcb_ct,TCBPRI_PAYOUT);
		}
		wk->sub_seq++;
		break;
	case 2:
		if(wk->eff_tcb_ct){
			return RET_NONE;
		}

#ifdef PM_DEBUG
		Debug_WinUpdate(wk);
#endif	//PM_DEBUG

		{
			BOOL is_bonus = SlotSub_IsBonusCode(wk->win_code);
			if(	!gsub_IsContinue(wk,is_bonus)){
				GameSeq_Change( wk ,SEQ_GAME_FORCE_END);
				return( RET_NONE );
			}
			if( is_bonus){
				GameSeq_Change( wk ,SEQ_TO_BONUS);
			}else{
				//ボーナスクレジット再描画
				SlotSub_GuideParamDraw( wk , STATE_NORMAL , BONUS_BLANK);
				GameSeq_Change( wk ,SEQ_GAME_END);
			}
		}
		return( RET_NONE );
	}
	return( RET_NONE );
}

//--------------------------------------------------------------
/**
 * ゲーム	ボーナスリーチ時負けエフェクト
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_LoseEff( SLOTMAIN *wk )
{
	int i;

	switch(wk->sub_seq){
	case 0:
		if(wk->bonus_reach != REACH_CODE_BLANK){	//ボーナスリーチの時だけもやもやアニメする
			//もやもや
			AddTCB_PictDraw(wk,PICT_SCRIB,&wk->eff_tcb_ct,TCBPRI_PAYOUT);
		}
		wk->sub_seq++;
		//ここはスルー
	case 1:
		if(wk->eff_tcb_ct){
			return RET_NONE;
		}
		wk->sub_seq++;
		//ここはスルー
	case 2:
		SlotSub_WinLoseIconSet( wk,gsub_DisconIconNumCheck(wk), FALSE );
		SlotSub_DooburuAnmSet( wk, DOOBURU_GRAD);
		wk->sub_seq++;
		break;
	case 3:
		if(wk->eff_tcb_ct){
			return RET_NONE;
		}
#ifdef PM_DEBUG
		Debug_WinUpdate(wk);
#endif	//PM_DEBUG

		if(	!gsub_IsContinue(wk,FALSE)){
			GameSeq_Change( wk ,SEQ_GAME_FORCE_END);
			return( RET_NONE );
		}
		if(wk->next_mode == MODE_BLANK){
			GameSeq_Change( wk ,SEQ_GAME_END);
		}else{
			GameSeq_Change( wk ,SEQ_GAME_MODE_CHANGE);
		}
		return( RET_NONE );
	}
	return( RET_NONE );
}

//--------------------------------------------------------------
/**
 * ゲーム	モードチェンジエフェクト
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_ModeChange( SLOTMAIN *wk )
{
	int i;

	switch(wk->sub_seq){
	case 0:
		AddTCB_BguModeChange(wk,wk->mode,wk->next_mode,&wk->eff_tcb_ct);
		wk->sub_seq++;
		break;
	case 1:
		if(wk->eff_tcb_ct){
			return RET_NONE;
		}
		wk->mode = wk->next_mode;
		wk->lot_fix_f = FALSE;	///大当たりフラグ落とす
		GameSeq_Change( wk ,SEQ_GAME_END);
		return( RET_NONE );
	}
	return( RET_NONE );
}


//--------------------------------------------------------------
/**
 * ゲーム	1ゲーム終了
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_End( SLOTMAIN *wk )
{
	if(wk->replay_f == FALSE){
		SlotSub_BetLineLightSetAll( wk, LIGHT_OFF );
		SlotSub_ReelLampSetAll( wk, LIGHT_OFF );
	}
	SlotSub_DooburuAnmSet( wk, DOOBURU_B2F);
	GameSeq_Change( wk ,SEQ_GAME_INIT);
	return( RET_NEXT );
}

//--------------------------------------------------------------
/**
 * ゲーム	ボーナスゲーム画面へ
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_ToBonus( SLOTMAIN *wk )
{
	switch(wk->sub_seq){
	case 0:
		gsub_StateChange( wk, STATE_BONUS );
		SlotSub_BetLineLightSetAll( wk, LIGHT_OFF );

		SlotSub_DooburuAnmSet( wk, DOOBURU_B2F );
		AddTCB_BguBackChange(wk,BGU_BACK_BONUS,&wk->eff_tcb_ct);
		AddTCB_PictOff( wk,&wk->eff_tcb_ct, TCBPRI_MONITER_EFF);
		AddTCB_ReelChange(wk,&wk->eff_tcb_ct,TCBPRI_MONITER_EFF);
		AddTCB_GuideChange(wk,STATE_BONUS,&wk->eff_tcb_ct,TCBPRI_MONITER_EFF);

		Snd_BgmStop(Snd_NowBgmNoGet(),8);
		wk->sub_seq++;
		break;
	case 1:
		if(wk->eff_tcb_ct){
			return RET_NONE;
		}
		Snd_BgmPlay(SEQ_GS_GAMEATARI);
		GameSeq_Change( wk ,SEQ_BONUS_INIT);
		break;
	}
	return( RET_NEXT );
}

//--------------------------------------------------------------
/**
 * ゲーム	通常ゲーム画面へ
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_ToNormal( SLOTMAIN *wk )
{
	switch(wk->sub_seq){
	case 0:
		gsub_StateChange( wk, STATE_NORMAL );

		AddTCB_BguBackChange(wk,BGU_BACK_HAPPY,&wk->eff_tcb_ct);
		AddTCB_PictOff( wk,&wk->eff_tcb_ct, TCBPRI_MONITER_EFF);
		AddTCB_ReelChange(wk,&wk->eff_tcb_ct,TCBPRI_MONITER_EFF);
		AddTCB_GuideChange(wk,STATE_NORMAL,&wk->eff_tcb_ct,TCBPRI_MONITER_EFF);
		Snd_BgmStop(Snd_NowBgmNoGet(),8);
		wk->sub_seq++;
		break;
	case 1:
		if(wk->eff_tcb_ct){
			return RET_NONE;
		}
		Snd_BgmPlay(SEQ_GS_GAME);
		GameSeq_Change( wk ,SEQ_GAME_END);
		break;
	}
	return( RET_NEXT );
}

//--------------------------------------------------------------
/**
 * ゲーム　ボーナスゲーム初期化
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET BonusGame_Init( SLOTMAIN *wk )
{
	int i;

	wk->payout = 0;
	wk->reel_speed = REEL_SPEED_B1;
	wk->game_cancel_f = FALSE;

	SlotSub_SysMsgPut(wk,MIDX_SYS_BET_BONUS);
	SlotSub_BackBtnWrite(wk,BACK_BTN_ACTIVE);

	for(i = 0;i < REEL_MAX;i++){
		wk->reel_auto[i] = FALSE;
	}

	GameSeq_Change( wk ,SEQ_BONUS_BET);
	return( RET_NEXT );
}

//--------------------------------------------------------------
/**
 * ボーナスゲーム	ベット
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET BonusGame_Bet( SLOTMAIN *wk )
{
	INPUT_RET ret;

	ret = Input_Check(wk,PAD_KEY_UP);
	if(ret == INPUT_NONE){
#ifdef PM_DEBUG
		Debug_WinVisibleCheck(wk);
#endif
		return RET_NONE;
	}
	if(ret == INPUT_END){
		GameSeq_ChangeNext( wk ,SEQ_GAME_EXIT_CHECK,SEQ_BONUS_BET);
		return RET_NONE;
	}
	wk->credit -= wk->bet;
	SlotSub_CoinNumDraw(wk,wk->credit,COIN_NUM_CREDIT);
	SlotSub_BetLineLightSetAll( wk, LIGHT_ON );
	SlotSub_ReelLampSetAll( wk, LIGHT_ON );

	//残っている絵を消去
	wk->eff_tcb_ct = 0;
	AddTCB_PictOff( wk, &wk->eff_tcb_ct, TCBPRI_MONITER_EFF );
	SlotSub_DooburuAnmSet( wk, DOOBURU_F2B );

	SlotSub_SysMsgPut(wk,MIDX_SYS_START);
	GameSeq_Change( wk ,SEQ_BONUS_START_WAIT);
	Snd_SePlay(SND_SLOT_BET);
	return( RET_NONE );
}

//--------------------------------------------------------------
/**
 * ボーナスゲーム	ゲーム開始待ち
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET BonusGame_StartWait( SLOTMAIN *wk )
{
	INPUT_RET ret;

	if(wk->eff_tcb_ct){
		return RET_NONE;
	}
	ret = Input_Check(wk,(PAD_KEY_UP|PAD_KEY_DOWN));
	if(ret == INPUT_NONE){
#ifdef PM_DEBUG
		Debug_WinVisibleCheck(wk);
#endif
		return RET_NONE;
	}
	if(ret == INPUT_END){
		wk->game_cancel_f = TRUE;
		GameSeq_ChangeNext( wk ,SEQ_GAME_EXIT_CHECK, SEQ_BONUS_START_WAIT);
		return RET_NONE;
	}
	//抽選
	GameSeq_Change( wk ,SEQ_BONUS_START);
	SlotSub_BackBtnWrite(wk,BACK_BTN_PASSIVE);
	return( RET_NEXT );
}

//--------------------------------------------------------------
/**
 * ボーナスゲーム	ゲーム開始
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET BonusGame_Start( SLOTMAIN *wk )
{
	INPUT_RET ret;

	//役抽選
	gsub_BonusLottery(wk);

#ifdef PM_DEBUG
	Debug_WinUpdate(wk);
	IWASAWA_PRINT("bonus lot = %d\n",wk->lot_basic);
#endif

#if 0
	wk->reel_reverce[0] = TRUE;
	wk->reel_reverce[1] = TRUE;
	wk->reel_reverce[2] = TRUE;

	wk->lot_basic = BONUS_CHIKO;
	wk->reel_speed = 3;
	wk->reel_auto[0] = 1;
	wk->reel_auto[1] = 2;
#endif
	//リール回転スタート
	ReelControlLotteryTargetSet( wk );
	ReelControlScrollSet( wk, 0);
	ReelControlScrollSet( wk, 1);
	ReelControlScrollSet( wk, 2);

	wk->eff_tcb_ct = 0;
	wk->bonus_reel_ct = 0;
	wk->bonus_reel_idx = 0;
	AddTCB_ExIconEff( wk,1,&wk->eff_tcb_ct,TCBPRI_MONITER_EFF);
	AddTCB_DraftDraw( wk,PICT_CHIKO+wk->lot_basic,&wk->eff_tcb_ct,TCBPRI_MONITER_EFF);
	SlotSub_GuideParamDraw( wk ,STATE_BONUS, wk->lot_basic );
	SlotSub_DooburuAnmSet( wk, DOOBURU_BURU );

	SlotSub_SysMsgPut(wk,MIDX_SYS_STOP);
	GameSeq_Change( wk ,SEQ_BONUS_AUTO_REEL_WAIT);
	Snd_SePlay(SND_SLOT_REEL_START);
	return( RET_NONE );
}

//--------------------------------------------------------------
/**
 * ボーナスゲーム	オートリールストップ待ち
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET BonusGame_AutoReelWait( SLOTMAIN *wk )
{
	int i;
	INPUT_RET ret;

	switch(wk->sub_seq){
	case 0:
		if(wk->eff_tcb_ct > 0){
			break;
		}
		for(i = 0;i < 3;i++){
			if(!wk->reel_auto[i]){
				continue;
			}
			SlotSub_ReelLampSet( wk,i,LIGHT_OFF );
			wk->bonus_reel_ct++;
		}
		if(wk->bonus_reel_ct == 0){	//オートで止まるリールはない
			GameSeq_Change( wk ,SEQ_BONUS_REEL_WAIT);
			return ( RET_NONE );
		}
		wk->wait = REEL_AUTO_STOP_WAIT;
		wk->sub_seq++;
		break;
	case 1:
		if(--wk->wait > 0){
			break;
		}
		for(i = 0;i < 3;i++){
			if(wk->reel_auto[i]){
				ReelControlStopSet( wk, i );
			}
		}
		wk->sub_seq++;
		break;
	case 2:
		for(i = 0;i < 3;i++){
			if(!wk->reel_auto[i]){
				continue;
			}
			if(wk->reel[i].scroll_flag){
				return RET_NONE;
			}
		}
	default:
		GameSeq_Change( wk ,SEQ_BONUS_REEL_WAIT);
		wk->sub_seq = 0;
		break;
	}
	return( RET_NONE );
}

//--------------------------------------------------------------
/**
 * ボーナスゲーム	手動リールストップ待ち
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET BonusGame_ReelWait( SLOTMAIN *wk )
{
	int i;
	INPUT_RET ret;

	switch(wk->sub_seq){
	case 0:
		//次に止めるリールを検索
		for(i = 0;i < REEL_MAX;i++){
			if(!wk->reel_auto[i]){
				wk->bonus_reel_idx = i;
				wk->bonus_reel_ct++;
				AddTCB_ReelLampAnimeSet( wk,i,TCBPRI_REEL_LAMP_ANM);
				break;
			}
		}
		wk->sub_seq++;
		//ここはスルー
	case 1:
		ret = Input_Check(wk,PAD_BUTTON_A);

		if(ret != INPUT_EXE){
			break;
		}
		SlotSub_ReelLampAnimeStopReq( wk, wk->bonus_reel_idx );
		ReelControlStopSet( wk, wk->bonus_reel_idx);
		wk->sub_seq++;
		break;
	case 2:
		if(wk->reel[wk->bonus_reel_idx].scroll_flag){
			return RET_NONE;
		}
		SlotSub_ReelLampSet( wk, wk->bonus_reel_idx,LIGHT_OFF );
		wk->reel_auto[wk->bonus_reel_idx] = TRUE;
		if(wk->bonus_reel_ct < REEL_MAX){
			wk->sub_seq = 0;
			break;
		}
		GameSeq_Change( wk ,SEQ_BONUS_REEL_SCROLL_WAIT);
		break;
	}
	return( RET_NONE );
}

//--------------------------------------------------------------
/**
 * ボーナスゲーム	リールの動作エフェクトが終了するまで待つ
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET BonusGame_ReelScrollWait( SLOTMAIN *wk )
{
	int i;

	for(i = 0;i < REEL_MAX;i++){
		if(wk->reel[i].scroll_flag){
			return( RET_NONE );
		}
	}
	SlotSub_IconEffVanishAll(wk);

	//Win/Loseチェック
	if(ReelCheckBonus( wk ,REEL_CHECK_WIN ) < 0){
		GameSeq_Change( wk ,SEQ_BONUS_LOSE_EFF);
	}else{
		wk->win_code = wk->lot_basic;
		wk->bonus_win_ct++;
		GameSeq_Change( wk, SEQ_BONUS_PAYOUT );
	}
	return( RET_NEXT );
}

//--------------------------------------------------------------
/**
 * ボーナスゲーム	コイン払い戻し
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET BonusGame_Payout( SLOTMAIN *wk )
{
	int i;

	switch(wk->sub_seq){
	case 0:
		//お絵かき開始
		AddTCB_PictDraw(wk,wk->win_code+PICT_CHIKO,&wk->eff_tcb_ct,TCBPRI_PAYOUT);
		AddTCB_GuideBlinkEff(wk,STATE_BONUS, wk->lot_basic ,&wk->eff_tcb_ct,TCBPRI_PAYOUT);
		for(i = 0;i < LINE_CODE_MAX;i++){
			AddTCB_HitLineEff(wk,i,NULL,TCBPRI_PAYOUT);
		}
		wk->sub_seq++;
		break;
	case 1:
		//お絵かき終了待ち
		if(wk->eff_tcb_ct){
			return RET_NONE;
		}
		//コイン払い戻しエフェクトタスク追加
		AddTCB_Payout(wk,
			wk->bonus_payout,&wk->eff_tcb_ct,TCBPRI_PAYOUT);
		//喜びアイコン表示セット
//		SlotSub_WinLoseIconSet( wk, TRUE );
		wk->sub_seq++;
		break;
	case 2:
		if(wk->eff_tcb_ct){
			return RET_NONE;
		}
		GameSeq_Change( wk ,SEQ_BONUS_END);
		return( RET_NONE );
	}
	return( RET_NONE );
}

//--------------------------------------------------------------
/**
 * ボーナスゲーム	負けエフェクト
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET BonusGame_LoseEff( SLOTMAIN *wk )
{
	int i;

	switch(wk->sub_seq){
	case 0:
		//もやもや
		AddTCB_PictDraw(wk,PICT_SCRIB,&wk->eff_tcb_ct,TCBPRI_PAYOUT);
		wk->sub_seq++;
		break;
	case 1:
		if(wk->eff_tcb_ct){
			return RET_NONE;
		}
		wk->eff_tcb_ct = 30;
		SlotSub_DooburuAnmSet( wk, DOOBURU_GRAD);
		wk->sub_seq++;
		break;
	case 2:
		if(--wk->eff_tcb_ct){
			return RET_NONE;
		}
		GameSeq_Change( wk ,SEQ_BONUS_END);
		return( RET_NONE );
	}
	return( RET_NONE );
}


//--------------------------------------------------------------
/**
 * ボーナスゲーム	1ゲーム終了
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET BonusGame_End( SLOTMAIN *wk )
{
	BOOL is_bonus = TRUE;

	wk->bonus_round++;

	if(wk->bonus_round >= wk->bonus_round_max){
		is_bonus = FALSE;
	}
	if(	!gsub_IsContinue(wk,is_bonus)){
		GameSeq_Change( wk ,SEQ_GAME_FORCE_END);
		return( RET_NONE );
	}
	if(!is_bonus){
		GameSeq_Change( wk ,SEQ_TO_NORMAL);
		return( RET_NEXT );
	}

	wk->bonus_payout = 0;
	SlotSub_GuideParamDraw( wk ,STATE_BONUS, BONUS_BLANK );

	SlotSub_BetLineLightSetAll( wk, LIGHT_OFF );
	SlotSub_ReelLampSetAll( wk, LIGHT_OFF );
	SlotSub_DooburuAnmSet( wk, DOOBURU_B2F);
	GameSeq_Change( wk ,SEQ_BONUS_INIT);
	return( RET_NEXT );
}



//--------------------------------------------------------------
/**
 * ゲーム	終了チェック
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_ExitCheck( SLOTMAIN *wk )
{
	INPUT_RET ret;

	switch(wk->sub_seq){
	case 0:
		SlotSub_BackBtnWrite(wk,BACK_BTN_ANIME);
		SlotSub_WinMsgPut(wk,MIDX_WIN_IS_END);
		//いったんアルファを切る
		G2S_SetBlendAlpha(GX_BLEND_PLANEMASK_NONE,GX_BLEND_PLANEMASK_NONE,31,0);
		G2S_SetBlendBrightness(BRIGHTNESS_MASK,BRIGHTNESS_VALUE);

		sub_YesNoBtnSet(wk);
		wk->sub_seq++;
		break;
	case 1:
		ret = sub_YesNoBtnWait(wk);

		if(ret == INPUT_NONE){
			return RET_NONE;
		}
		if(ret == INPUT_END){
			GameSeq_Change( wk ,SEQ_GAME_EXIT );
		}else{
			SlotSub_WinMsgClear(wk);
			SlotSub_BackBtnWrite(wk,BACK_BTN_ACTIVE);
			//ブレンド設定を戻す
			G2S_SetBlendBrightness(GX_BLEND_PLANEMASK_NONE,0);
			G2S_SetBlendAlpha(ALPHA_MASK_1ST,ALPHA_MASK_2ND,ALPHA_SECOND,ALPHA_FIRST);

			GameSeq_Change( wk , wk->game_next_seq );
		}
		break;
	}
	return( RET_NONE );
}

//--------------------------------------------------------------
/**
 * ゲーム	強制終了チェック
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_ForceEnd( SLOTMAIN *wk )
{
	switch(wk->sub_seq){
	case 0:
		SlotSub_WinMsgPut(wk,wk->force_end_code);
		//いったんアルファを切る
		G2S_SetBlendAlpha(GX_BLEND_PLANEMASK_NONE,GX_BLEND_PLANEMASK_NONE,31,0);
		G2S_SetBlendBrightness(BRIGHTNESS_MASK,BRIGHTNESS_VALUE);
		wk->sub_seq++;
		break;
	case 1:
		if(sys.trg & PAD_BUTTON_KTST_CHG){
			wk->key_mode = APP_KTST_KEY;
		}
		if(sys.trg & (PAD_BUTTON_DECIDE|PAD_BUTTON_CANCEL)){
			GameSeq_Change( wk ,SEQ_GAME_EXIT );
		}else if(GF_TP_GetTrg()){
			wk->key_mode = APP_KTST_TOUCH;
			GameSeq_Change( wk ,SEQ_GAME_EXIT );
		}
		break;
	}
	return( RET_NONE );
}

//--------------------------------------------------------------
/**
 * ゲーム	終了処理
 * @param	main	SLOTMAIN
 * @retval	GAMERET	GAMERET
 */
//--------------------------------------------------------------
static GAMERET Game_Exit( SLOTMAIN *wk )
{
	if(wk->game_cancel_f){	//キャンセルされたので掛け金を戻す
		wk->credit += wk->bet;
	}
	//終了処理
	return( RET_END );
}

//--------------------------------------------------------------
///	スロットゲーム動作テーブル
//--------------------------------------------------------------
static GAMERET (* const DATA_SlotGameTbl[SEQ_MAX])( SLOTMAIN *wk ) =
{
	//通常ゲーム
	Game_Init,
	Game_InitEff,
	Game_Bet,
	Game_StartWait,
	Game_Start,
	Game_ReelWait0,
	Game_ReelWait1,
	Game_ReelWait2,
	Game_ReelScrollWait,
	Game_Payout,
	Game_LoseEff,
	Game_ModeChange,
	Game_End,
	Game_ToBonus,
	Game_ToNormal,
	BonusGame_Init,
	BonusGame_Bet,
	BonusGame_StartWait,
	BonusGame_Start,
	BonusGame_AutoReelWait,
	BonusGame_ReelWait,
	BonusGame_ReelScrollWait,
	BonusGame_Payout,
	BonusGame_LoseEff,
	BonusGame_End,
	Game_ExitCheck,
	Game_ForceEnd,
	Game_Exit,
};

///////////////////////////////////////////////////////////////
//ゲームサブ
///////////////////////////////////////////////////////////////

/*
 *	@brief	残コイン数が足りるか？
 */
static BOOL gsub_IsContinue( SLOTMAIN* wk ,BOOL is_bonus)
{
	if(wk->credit >= COIN_MAX){
		wk->force_end_code = MIDX_WIN_COIN_FULL;
		return FALSE;
	}
	if(wk->replay_f){
		return TRUE;
	}
	if(is_bonus){
		if(wk->credit < COIN_BET_BONUS){
			wk->force_end_code = MIDX_WIN_COIN_NG0;
			return FALSE;
		}
	}else if(wk->credit < COIN_BET_DEF){
		wk->force_end_code = MIDX_WIN_COIN_NG3;
		return FALSE;
	}
	return TRUE;
}

/**
 *	@brief	ボーナスゲーム初期化パラメータセット
 */
static void gsub_StateChange( SLOTMAIN* wk, GAME_STATE state)
{
	int i,tmp;

	if(state == STATE_NORMAL){
		wk->bonus_game_f = FALSE;
		wk->bonus_round_max = 0;
		wk->mode = MODE_HAPPY;

		if(wk->consec_win <= CONSEC_WIN_MAX){
			tmp = DATA_BonusEndStressCorrectTable[wk->consec_win/3];
		}else{
			tmp = DATA_BonusEndStressCorrectTable[2];
		}
		wk->stress_ct = -(tmp+(wk->bonus_win_ct*BONUS_END_STRESS_CORRECT_NUM));

		wk->consec_f = TRUE;
		if(wk->consec_win < CONSEC_WIN_CT_MAX){
			wk->consec_win++;	//カンストは10
		}
	}else{
		wk->bonus_game_f = TRUE;
		wk->bet = COIN_BET_BONUS;

		if(wk->win_code == PRIZE_BIG){
			wk->bonus_round_max = BONUS_ROUND_BIG;
			RECORD_Inc(wk->rec_sv, RECID_SLOT_FEAVER );
		}else{
			wk->bonus_round_max = BONUS_ROUND_REG;
		}

	}
	wk->bonus_credit[REACH_REG] = 0;
	wk->bonus_credit[REACH_BIG] = 0;

	wk->lot_fix_f = FALSE;
	wk->bonus_round = 0;
	wk->bonus_win_ct = 0;
	wk->bonus_get_ct = 0;
	wk->bonus_payout = 0;
	for(i = 0;i < REEL_MAX;i++){
		wk->reel_auto[i] = FALSE;
		wk->reel_reverce[i] = FALSE;
	}

#ifdef PM_DEBUG
	if(wk->deb_print_f){
		wk->deb_lot_basic = 0;
		wk->deb_lot_ex = 0;
		Debug_WinUpdate(wk);
	}
#endif
}

/**
 *	@brief	抽選を行う
 */
static void gsub_Lottery(SLOTMAIN* wk)
{
	int i;
	u16 *b_tbl,*e_tbl;
	s16	rnd,prob;

	b_tbl = (u16*)&DATA_BasicLotTable[wk->hard].prob[wk->mode][0];

	//基本抽選
	rnd = SlotSub_Rand1000();
	prob = PROB_VALUE;

	if(!wk->lot_fix_f){
		wk->lot_basic = PRIZE_BLANK;
		for(i = 0;i < PRIZE_CODE_MAX;i++){
			prob -= b_tbl[i];
			if(rnd >= prob){
				wk->lot_basic = i;
				break;
			}
		}
	}
#ifdef PM_DEBUG
	if(wk->deb_print_f && wk->deb_lot_basic != 0){
		wk->lot_basic = wk->deb_lot_basic - 1;
	}
#endif
	IWASAWA_PRINT("LotBasic = %d\n",wk->lot_basic);
	//基本抽選を固定するかどうかチェック
	if(SlotSub_IsBonusCode(wk->lot_basic)){
		wk->lot_fix_f = TRUE;
	}

	rnd = SlotSub_Rand1000();
	e_tbl = (u16*)(&DATA_ExLotTable[wk->lot_basic].prob[wk->mode][0]);

	if(wk->lot_basic >= PRIZE_BONG){
		if(rnd >= PROB_VALUE-e_tbl[0]){
			wk->lot_ex = 0;
		}else{
			wk->lot_ex = 1;
		}
	}else{
		if(rnd >= PROB_VALUE-e_tbl[0]){
			wk->lot_ex = 0;
		}else if(rnd >= (PROB_VALUE-(e_tbl[0]+e_tbl[1]))){
			wk->lot_ex = 1;
		}else{
			wk->lot_ex = 2;
		}
	}
#ifdef PM_DEBUG
	if(wk->deb_print_f && wk->deb_lot_basic != 0 && wk->deb_lot_ex != 0){
		wk->lot_ex = wk->deb_lot_ex - 1;
	}
#endif
}

/**
 *	@brief	リーチ抽選を行う
 */
static int gsub_ReachCheck( SLOTMAIN* wk )
{
	int i;

	wk->hit_num = ReelCheck(wk,REEL_CHECK_REACH,wk->reach_code,wk->hit_line);

	if(wk->hit_num == 0){
		return  0;
	}
	wk->bonus_reach = REACH_CODE_BLANK;
	for(i = 0;i < wk->hit_num;i++){
		if( SlotSub_IsBonusCode(wk->reach_code[i]) ){
			wk->bonus_reach = wk->reach_code[i]-PRIZE_REG;
			break;
		}
	}
	if(wk->bonus_reach == REACH_CODE_BLANK){
		return 0;
	}
	//ダブルリーチ以上、または子役当選の時は、スーパーリーチにならない
	if(	wk->hit_num > 1 || (!SlotSub_IsBonusCode(wk->lot_basic)) ){
		return 1;
	}
	//リーチモード抽選
	{
		u16 rnd = SlotSub_Rand(100);

		if(rnd < DATA_SuperReachTable[wk->mode][wk->lot_basic]){
			wk->super_reach_f = TRUE;
		}
	}
	return (wk->super_reach_f+1);
}

/**
 *	@brief	ボーナスゲームの抽選を行う
 */
static void gsub_BonusLottery(SLOTMAIN* wk)
{
	int i;
	s16	rnd;
	u8	consec_ct,lv;
	const BONUS_GAME_PARAM* prm;

	if(wk->consec_win > CONSEC_WIN_MAX){
		consec_ct = CONSEC_WIN_MAX;
	}else{
		consec_ct = wk->consec_win;
	}
	if(wk->bonus_round_max == BONUS_ROUND_BIG){
		if(wk->bonus_round < BIG_BONUS_LV_LIM1){
			lv = 0;
		}else if(wk->bonus_round < BIG_BONUS_LV_LIM2){
			lv = 1;
		}else{
			lv = 2;
		}
	}else{
		if(wk->bonus_round < REG_BONUS_LV_LIM1){
			lv = 0;
		}else if(wk->bonus_round < REG_BONUS_LV_LIM2){
			lv = 1;
		}else{
			lv = 2;
		}

	}
	prm = &(DATA_BonusLotTable[consec_ct].lv[lv]);

	//絵柄抽選
	wk->lot_basic = SlotSub_Rand(BONUS_CODE_MAX);

#ifdef PM_DEBUG
	if(wk->deb_print_f && wk->deb_lot_basic != 0){
		wk->lot_basic = wk->deb_lot_basic - 1 ;
	}
#endif

	//払い出し金額セット
	wk->bonus_payout = SlotSub_GetPayoutCoinNum(wk, wk->lot_basic);

	//ドラムの回転方向抽選
	for(i = 0;i < REEL_MAX;i++){
		rnd = SlotSub_Rand(100);
		if(rnd < prm->dir){
			wk->reel_reverce[i] = FALSE;
		}else{
			wk->reel_reverce[i] = TRUE;
		}
	}

	//ドラムのスピード抽選
	rnd = SlotSub_Rand(100);
	if(rnd < prm->speed[0]){
		wk->reel_speed = REEL_SPEED_B1;
		wk->reel_speed_id = 0;
	}else if(rnd < (prm->speed[0]+prm->speed[1])){
		wk->reel_speed = REEL_SPEED_B2;
		wk->reel_speed_id = 1;
	}else{
		wk->reel_speed = REEL_SPEED_B3;
		wk->reel_speed_id = 2;
	}

	//自動停止するリール数抽選
	rnd = SlotSub_Rand(100);

	if(rnd < prm->auto_stop[0]){
		rnd = SlotSub_Rand(REEL_MAX);
		for(i = 0; i < REEL_MAX;i++){
			if(rnd != i){
				wk->reel_auto[i] = TRUE;
			}else{
				wk->reel_auto[i] = FALSE;
			}
		}
	}else if(rnd < (prm->auto_stop[0]+prm->auto_stop[1])){
		rnd = SlotSub_Rand(REEL_MAX);
		for(i = 0; i < REEL_MAX;i++){
			if(rnd == i){
				wk->reel_auto[i] = TRUE;
			}else{
				wk->reel_auto[i] = FALSE;
			}
		}
	}else{
		for(i = 0; i < REEL_MAX;i++){
			wk->reel_auto[i] = FALSE;
		}
	}
}

/**
 *	@brief	当選時の喜びアイコン数抽選
 */
static int gsub_GradIconNumCheck( SLOTMAIN* wk )
{
	int i,num;
	u16	rnd,tmp;

	tmp = 100;
	num = 3;
	rnd = SlotSub_Rand(100);

	for(i = 0;i < WL_ICON_MAX;i++){
		tmp -= DATA_GradIconNumTable[wk->win_code][i];
		if(rnd >= tmp){
			num = i;
			break;
		}
	}
	//子役当選ならボーナスクレジット加算
	if( SlotSub_IsBonusCode(wk->win_code) || num == 0){
		return num;
	}

	for(i = 0;i < 2;i++){
		wk->bonus_credit[i] += DATA_BonusCreditTable[wk->hard][i]*num;
		if(wk->bonus_credit[i] > BONUS_CREDIT_MAX){
			wk->bonus_credit[i] = BONUS_CREDIT_MAX;
		}
	}
	return num;
}

/**
 *	@brief	落選時の不満アイコン数抽選
 */
static int gsub_DisconIconNumCheck( SLOTMAIN* wk )
{
	int i,num;
	u16	rnd,tmp;

	wk->next_mode = MODE_BLANK;

	if(wk->bonus_reach == REACH_CODE_BLANK || wk->stress_ct <= 0){
		if((!wk->bonus_game_f) && (wk->stress_ct <= 0)){
			wk->stress_ct += DATA_StressAddTable[wk->mode];
		}
#ifdef DEBUG_ONLY_FOR_iwasawa
		return 3;
#else
		return 0;
#endif
	}

	tmp = 100;
	num = 3;
	rnd = SlotSub_Rand(100);

	for(i = 0;i < WL_ICON_MAX;i++){
		tmp -= DATA_DisconIconNumTable[(wk->super_reach_f*2)+wk->bonus_reach][i];
		if(rnd >= tmp){
			num = i;
			break;
		}
	}

	//ストレスカウンター加算
	wk->stress_ct += DATA_StressAddTableDisconIcon[wk->mode]*num;
	if(wk->stress_ct > 99)	{
		wk->stress_ct = 99;
	}

	if(wk->stress_ct <= 0){
#ifdef DEBUG_MODE_CHANGE_FORCE
//		return num;
#else
		return num;
#endif
	}
	//モード移行抽選
	rnd = SlotSub_Rand(100);
	tmp = (DATA_ModeChangeLotTable[wk->super_reach_f][wk->hard][wk->mode]*wk->stress_ct)/MODE_CHAGNE_CORRECT_NUM;
	if(rnd >= tmp){
#ifdef DEBUG_MODE_CHANGE_FORCE
//		return num;	//モード移行なし
#else
		return num;	//モード移行なし
#endif
	}

	//どのモードに移行するか
	rnd = SlotSub_Rand(100);

	wk->next_mode = MODE_SLAMP;
	tmp = 100;
	for(i = 0;i < MODE_MAX;i++){
		tmp -= DATA_NextModeLotTable[wk->mode][wk->hard][i];
		if(rnd >= tmp){
			wk->next_mode = i;
			break;
		}
	}
	wk->stress_ct = (wk->stress_ct*(-1)*DATA_ModeChangeStressCorrectTable[wk->mode][wk->next_mode])/10;

	wk->consec_win = 0;	//連荘クリア
	wk->consec_f = FALSE;	//連荘クリア

#ifdef PM_DEBUG
	Debug_WinUpdate(wk);
#endif
	return num;
}

///////////////////////////////////////////////////////////////
//描画系サブ
///////////////////////////////////////////////////////////////
/**
 *	@brief	YesNoボタン初期化
 */
static void sub_YesNoBtnSet(SLOTMAIN* wk)
{
	TOUCH_SW_PARAM param;
	MI_CpuClear8(&param,sizeof(TOUCH_SW_PARAM));

	param.p_bgl  = wk->bgl;
	param.bg_frame = BMP_YESNO_FRM;
	param.char_offs = BMP_YESNO_CGX;
	param.pltt_offs = BMP_YESNO_PAL;
	param.x		= BMP_YESNO_PX;
	param.y		= BMP_YESNO_PY;
	param.kt_st = wk->key_mode;
	param.key_pos = 1;
	param.type = TOUCH_SW_TYPE_S;
//	TOUCH_SW_InitEx( wk->ynbtn_wk, &param , NULL/*wk->palAnm*/);
	TOUCH_SW_Init( wk->ynbtn_wk, &param );
}

/**
 *	@brief	YesNoボタン待ち
 */
static int sub_YesNoBtnWait(SLOTMAIN* wk)
{
	int ret;
	int	mode;

	ret = TOUCH_SW_Main( wk->ynbtn_wk );

	switch(ret){
	case TOUCH_SW_RET_YES:
		ret = INPUT_END;
		break;
	case TOUCH_SW_RET_NO:
		ret = INPUT_EXE;
		break;
	default:
		return INPUT_NONE;
	}
	wk->key_mode = TOUCH_SW_GetKTStatus(wk->ynbtn_wk);
	TOUCH_SW_Reset( wk->ynbtn_wk);


	return ret;
}

